package com.heima.wemedia.controller;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.model.user.dtos.AuthDto;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/page")
    public PageResult<WmNewsResultDTO> findByPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findByPage(dto);
    }

    /**
     * 保存草稿,提交审核,修改
     *
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto) {
        wmNewsService.submit(dto);
        return ResponseResult.ok();
    }



    /**
     * 自媒体文章上下架
     *
     * @param dto
     * @return
     */
    @PutMapping("/api/v1/news/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        wmNewsService.downOrUp(dto);
        return ResponseResult.ok();
    }

    /**
     * 分页媒体审核
     *
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news_auth/list")
    public PageResult<WmNewsResultDTO> list(@RequestBody ChannelDto dto) {
        return wmNewsService.findByAllPage(dto);
    }


    /**
     * 文章详情--(id查询)
     *
     * @param id
     * @return
     */
    @GetMapping("/api/v1/news_auth/one/{id}")
    WmNewsResultDTO findById(@PathVariable("id") Integer id) {
        return wmNewsService.findByIdWmNewsDto(id);
    }


    /**
     * 审核不通过
     *
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news_auth/auth_fail")
    public void updateStatusNo(@RequestBody AuthDto dto) {

        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(2);
        wmNews.setReason(dto.getMsg());
        wmNewsService.updateById(wmNews);
    }

    /**
     * 审核通过
     *
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news_auth/auth_pass")
    public void updateStatusOk(@RequestBody AuthDto dto) {



        wmNewsService.setArticleByDto(dto);
    }


    /**
     * 根据id 查询自媒体文章
     * @param id
     * @return
     */
    @GetMapping("/api/v1/news/one/{id}")
    public ResponseResult<ArticleDto> selectOneArticle(@PathVariable("id") Integer id) {
        return ResponseResult.ok(wmNewsService.selectOneArticle(id));
    }


    /**
     * 删除自媒体文章
     * @param id
     * @return
     */
    @DeleteMapping("/api/v1/news/del_news/{id}")
    public ResponseResult deleteByIdArticle(@PathVariable("id") Integer id){
        wmNewsService.deleteByIdArticle(id);
        return ResponseResult.ok();
    }
}
